<template>
    <view>
        <numberForm ref="numberForm" @autoCheckFormData="autoCheckFormData"></numberForm>
        <button type="primary" class="btn" @click="register">注册</button>
        <button type="default" class="btn" @click="toLogin">登录</button>
    </view>
</template>

<script>
    import numberForm from '@/components/number-form/number-form'
    import {
        Register
    } from '@/services/MemberService'
    import {
        getTime
    } from "@/utils/utils.js"
    export default {
        components: {
            numberForm
        },
        data() {
            return {
                formData: {
                    number: '',
                    password: ''
                }
            }
        },
        methods: {
            autoCheckFormData: function(data) {
                this.formData = data
            },
            register: function() {
                let checkRes = this.$refs.numberForm.validataForm()
                if (checkRes) {
                    this.asyncRegister()
                }
            },
            async asyncRegister() {
                this.showLoading()
                let registerRes = await Register({
                    number: this.formData.number,
                    password: this.formData.password,
                    timestamp: getTime()
                });
                if (registerRes.code) {
                    this.hideLoading()
                    this.tostMsg(registerRes.msg, 'error')
                    return false;
                }
                this.hideLoading()
                // #ifndef H5
                uni.setStorageSync('isLogin', 'true')
                uni.setStorageSync('memberToken', registerRes.datas.token)
                uni.switchTab({
                    url: '/pages/home/home'
                })
                // #endif
                // #ifdef H5
                sessionStorage.setItem('isLogin','true')
                sessionStorage.setItem('memberToken',registerRes.datas.token)
                uni.switchTab({
                    url: '/home'
                })
                // #endif
            },
            toLogin: function() {
                // #ifndef H5
                uni.reLaunch({
                    url: '/pages/number/login'
                })
                // #endif
                // #ifdef H5
                uni.navigateTo({
                    url: '/'
                })
                // #endif
            }
        }
    }
</script>

<style lang="scss" scoped>
    .btn {
        width: 42%;
        margin: 0 auto;

        margin: {
            bottom: 30rpx;
            left: 5%;
        }

        display: inline-block;
    }
</style>